package ru.lukyanets.transmission.client.util;

/**
 * @author Sergey Lukyanets
 */
public class UnitsUtil {
    @SuppressWarnings("WeakerAccess")
    public static final long KiB = 1024L;
    @SuppressWarnings("WeakerAccess")
    public static final long MiB = KiB * 1024L;
    @SuppressWarnings("WeakerAccess")
    public static final long GiB = MiB * 1024L;

    public static String getSpeedText(double speed) {
        return getSpeedText(speed, 1);
    }

    public static String getSpeedText(double speed, int precision) {
        String format = "%." + precision + "f";
        String speedText;
        if (speed > GiB) {
            speedText = String.format(format, speed / GiB) + " " + LocalizationUtil.getString("speed.gbs");
        } else if (speed > MiB) {
            speedText = String.format(format, speed / MiB) + " " + LocalizationUtil.getString("speed.mbs");
        } else {
            speedText = String.format(format, speed / KiB) + " " + LocalizationUtil.getString("speed.kbs");
        }
        return speedText;
    }

    public static String getSizeText(double size) {
        return getSizeText(size, 2);
    }

    public static String getSizeText(double size, int precision) {
        String format = "%." + precision + "f";
        String sizeText;
        if (size > GiB) {
            sizeText = String.format(format, size / GiB) + " " + LocalizationUtil.getString("size.gb");
        } else if (size > MiB) {
            sizeText = String.format(format, size / MiB) + " " + LocalizationUtil.getString("size.mb");
        } else {
            sizeText = String.format(format, size / KiB) + " " + LocalizationUtil.getString("size.kb");
        }
        return sizeText;
    }

    public static String getPercentText(double percent) {
        return getPercentText(percent, 1);
    }

    public static String getPercentText(double percent, int precision) {
        return String.format("%." + precision + "f%%", percent * 100.0);
    }
}
